package com.mrx.spring.example.advice;

import com.mrx.commons.model.dto.Result;
import com.mrx.commons.task.exception.TaskExecuteException;
import com.mrx.commons.utils.StringPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Mr.X
 * @since 2023-12-09 下午 12:06:30
 */
@RestControllerAdvice
public class XScheduleTaskAdvice {

    private static final Logger logger = LoggerFactory.getLogger(XScheduleTaskAdvice.class);

    @ExceptionHandler(TaskExecuteException.class)
    public Result<?> taskExecuteExceptionHandler(TaskExecuteException e) {
        StringPrinter printer = new StringPrinter();
        e.printStackTrace(printer);
        logger.warn("任务执行失败: ", e);
        return Result.fail(printer.toString());
    }

}
